package com.zhoug.zmui.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.ints.IDismissListenerWindow;
import com.zhoug.zmui.ints.OnDismissListener;
import com.zhoug.zmui.utils.ZMUIConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * PopupWindow基类
 * @Author 35574
 * @Date 2020/7/27
 * @Description
 */
public class ZMUIBasePopupWindow extends PopupWindow implements IDismissListenerWindow {
    private static final String TAG = ">>>ZMUIBasePopupWindow";
    private static final boolean DEBUG = false;
    private static final int DEFAULT_ANCHORED_GRAVITY = Gravity.TOP | Gravity.START;
    public static final float DIM_AMOUNT_NOT_EXIST = -1f;
    private Context mContext;
    private WindowManager _mWindowManager;
    //遮罩透明度
    private float mDimAmount = DIM_AMOUNT_NOT_EXIST;

    /**
     * 关闭窗口监听
     */
    private List<com.zhoug.zmui.ints.OnDismissListener> onDismissListeners;


    public ZMUIBasePopupWindow(Context context) {
        //设置默认宽高
        // 防止在部分机型上不显示是因为没有设置宽高,必须2个都设置才行
        this(context, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public ZMUIBasePopupWindow(Context context, int width, int height) {
        super(width, height);
        this.mContext = context;
        // 如果不设置这句代码,点击 pop 以外区域是不会被 dismiss 的.
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setFocusable(true);//设置获取焦点,点击返回键才会关闭窗口
        setTouchable(true);//设置true,点击窗口外部才能关闭窗口
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if(onDismissListeners!=null && onDismissListeners.size()>0){
                    for(int i=0;i<onDismissListeners.size();i++){
                        com.zhoug.zmui.ints.OnDismissListener onDismissListener = onDismissListeners.get(i);
                        if(onDismissListener!=null){
                            onDismissListener.onDismiss();
                        }
                    }
                }
            }
        });
    }


    public Context getContext() {
        return mContext;
    }

    /**
     * 获取视图
     *
     * @return
     */
    protected View getDecorView() {
        View decorView = null;
        try {
            if (getBackground() == null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    decorView = (View) getContentView().getParent();
                } else {
                    decorView = getContentView();
                }
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    decorView = (View) getContentView().getParent().getParent();
                } else {
                    decorView = (View) getContentView().getParent();
                }
            }
        } catch (Exception ignore) {

        }
        return decorView;
    }


    /**
     * 更新遮罩透明度
     */
    private void updateDimAmount() {
        if (mDimAmount != DIM_AMOUNT_NOT_EXIST) {
            View decorView = getDecorView();
            if (decorView != null) {
                WindowManager.LayoutParams p = (WindowManager.LayoutParams) decorView.getLayoutParams();
                p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                p.dimAmount = mDimAmount;
                _getWindowManager().updateViewLayout(decorView, p);
            }
        }

    }

    /**
     * 获取窗口管理器
     *
     * @return
     */
    protected WindowManager _getWindowManager() {
        if (_mWindowManager == null) {
            _mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        }
        return _mWindowManager;
    }



    @Override
    public void showAsDropDown(View anchor) {
        this.showAsDropDown(anchor, 0, 0);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        this.showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);
    }

    /**
     * 在锚点anchor的正下方弹出popup，参数xoff和yoff分别是x轴和y轴的偏移量，
     * 偏移量是相对锚点anchor来说的，以anchor的左下角为参考点
     * @param anchor
     * @param xoff
     * @param yoff
     * @param gravity
     */
    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        updateDimAmount();
    }

    /**
     * 这个方法是对于整个window的屏幕以坐标来定位置的，不存在相对于某一个view，
     * 有相对也是相对整个屏幕，参数x和y是坐标
     * @param parent
     * @param gravity
     * @param x
     * @param y
     */
    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        updateDimAmount();
    }

    /**
     * 设置遮罩
     *
     * @param dimAmount
     * @return
     */
    public ZMUIBasePopupWindow setDimAmount(float dimAmount) {
        mDimAmount = dimAmount;
        return this;
    }

    /**
     * 设置遮罩
     * @return
     */
    public ZMUIBasePopupWindow setDimAmount() {
        if (mDimAmount == DIM_AMOUNT_NOT_EXIST) {
            //主体中配置的默认透明度
            float defDimAmount = ResourceUtils.getAttrFloatValue(mContext, R.attr.zmui_dimAmount);
            if (defDimAmount >0) {
                mDimAmount = defDimAmount;
            }else{
                mDimAmount= ZMUIConstant.DIM_AMOUNT;
            }
            if (DEBUG) {
                LogUtils.d(TAG, "setDimAmount:" + mDimAmount);
            }
        }
        return this;
    }

    @Override
    public void setContentView(View contentView) {
        super.setContentView(contentView);
    }

    /**
     * @see {{@link #addOnDismissListener(com.zhoug.zmui.ints.OnDismissListener)}}
     * @param onDismissListener
     */
    @Deprecated
    @Override
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        super.setOnDismissListener(onDismissListener);
    }

    @Override
    public void addOnDismissListener(com.zhoug.zmui.ints.OnDismissListener onDismissListener) {
        if(onDismissListener==null) return;
        if (this.onDismissListeners == null) {
            onDismissListeners = new ArrayList<>();
        }
        if (!onDismissListeners.contains(onDismissListener)) {
            onDismissListeners.add(onDismissListener);
        }
    }

    @Override
    public void removeOnDismissListener(com.zhoug.zmui.ints.OnDismissListener onDismissListener) {
        if(onDismissListener==null) return;
        if(onDismissListeners!=null && onDismissListeners.size()>0){
            onDismissListeners.remove(onDismissListener);
        }
    }



}
